/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 * Created on 6.5.2011, 13:15:14
 */

package src.clovece;

import java.awt.Point;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import src.clovece.game.GameInfo;
import src.clovece.game.Load;

/**
 *
 * @author Thomas
 */
public class LoadSingleplayerGameDialog extends javax.swing.JDialog {
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();
    private GameFrame parent = null;
    protected ArrayList<String> al = new ArrayList<String>();
    protected String selected;
    
    /** Creates new form LoadSingleplayerGameDialog */
    public LoadSingleplayerGameDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = (GameFrame) parent;
        Point p = parent.getLocation();
        this.setLocation(p.x +300, p.y +150);

    }

     public void showYourself() {
        initComponents();
        findSaveFiles();
        this.setVisible(true);


    }

    public void findSaveFiles()
    {
        final Collection<File> all = new ArrayList<File>();
        File file = new File(System.getProperty("user.dir")+ System.getProperty("file.separator")
                      + "examples" + System.getProperty("file.separator") + "save"
                      + System.getProperty("file.separator"));


        final File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                all.add(child);
            }
         }

            String patternStr = "^[\\S\\s]+[\\\\\\/]([\\S\\s]+.svg)$";
            Pattern pattern = Pattern.compile(patternStr);
            Matcher matcher = null;
            boolean matchFound = false;

            
            Iterator iterator = all.iterator();
            for (;iterator.hasNext();)
             {
                matcher = pattern.matcher(iterator.next().toString());
                matchFound = matcher.find();

                if (matchFound) {
                    int j = 0;
                    for (int i=0; i<=matcher.groupCount()-1; i++, j++) {
                         this.al.add(j,matcher.group(i+1));
                    }
                }
             }
            
            List.setListData(al.toArray());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LoadGameButton = new javax.swing.JButton();
        BackButton = new javax.swing.JButton();
        SaveGameLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        List = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        LoadGameButton.setText("Load");
        LoadGameButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LoadGameButtonMouseClicked(evt);
            }
        });

        BackButton.setText("Back");
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

        SaveGameLabel.setText("Saved Games");

        List.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        List.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                ListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(List);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(LoadGameButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(BackButton)
                        .addGap(39, 39, 39))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(SaveGameLabel)
                        .addContainerGap(148, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SaveGameLabel)
                .addGap(26, 26, 26)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BackButton)
                    .addComponent(LoadGameButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_BackButtonActionPerformed

    private void ListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_ListValueChanged
        int i = List.getSelectedIndex();
        selected = this.al.get(i);
    }//GEN-LAST:event_ListValueChanged

    private void LoadGameButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LoadGameButtonMouseClicked
        if(this.List.getSelectedValue() != null) {
            Load load = Load.getLoadInstance();
            load.LoadGame(this.List.getSelectedValue().toString());
            this.gameInfo.setLoadedGame(true);
            this.gameInfo.setNewGame(false);
            this.gameInfo.setJoinedGame(false);
            this.gameInfo.setLocalGame(true);
            this.parent.fillGameFrame();
            this.dispose();
        }
    }//GEN-LAST:event_LoadGameButtonMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BackButton;
    private javax.swing.JList List;
    private javax.swing.JButton LoadGameButton;
    private javax.swing.JLabel SaveGameLabel;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
